package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qf.Resp.BaseResp;
import com.qf.borrower.Borrower;
import com.qf.client.MessageClient;
import com.qf.loanapplication.Loanapplication;
import com.qf.loanrepayment.Loanrepayment;
import com.qf.service.UsermessageService;
import com.qf.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UsermessageController {

    @Autowired
    UsermessageService usermessageService;
    @Autowired
    MessageClient messageClient;
    @Autowired
    JWTUtils jwtUtils;

    @RequestMapping("/findUsermessageAll")
    public List<Borrower> findUsermessageAll(){
        return usermessageService.findUsermessageAll();
    }

    @RequestMapping("/findUsermessageById")
    public BaseResp findUsermessageById(HttpServletRequest req){

        //从tok中取借款人ID
        /*Cookie[] cookies = req.getCookies();
        String token = null;
        if (cookies != null) {
            token = getToken(cookies);
        } else {
            System.out.println("token为null");
        }
        Map verify = jwtUtils.Verify(token);
        Borrower o = (Borrower) verify.get("body");
        int borrowerId = o.getId();*/
        int borrowerId =1;

                //1.查询信息   2020-10-31 16:00:00/借50000元/3个月
        Map map = new HashMap();
        map.put("borrowerId",borrowerId);
        BaseResp baseResp1 = messageClient.selectAll(map);//调别人的接口


        List<Loanapplication> listRes1 = (List<Loanapplication>)baseResp1.getMessage();
        String jsons = JSON.toJSONString(listRes1);
        System.out.println("listRes="+listRes1);
        System.out.println("jsons="+jsons);
        List<Loanapplication> list = (List<Loanapplication>) JSONArray.parseArray(jsons, Loanapplication.class);
        System.out.println("list====="+list);
        //2.查询进度
        List list2 = new ArrayList();
        BaseResp baseResp = new BaseResp();
        for(Loanapplication loanapplication : list){
            Map map2  = new HashMap<String ,String>();
            map2.put("createTime",loanapplication.getCreateTime());
            map2.put("loanAmount",loanapplication.getLoanAmount());
            map2.put("loanDuration",loanapplication.getLoanDuration());

            int id = loanapplication.getId();//1 2
            System.out.println("id=========="+id);
            String status = "";
            String str1 = loanapplication.getStatus();
            if (!str1.equals("打款成功")){
                status = str1;
            }else {
                Loanrepayment loanrepayment = usermessageService.findUsermessageById(id);//loanrepayment;联合
                status = loanrepayment.getPaymentStatus();
            }
            System.out.println("status==============="+status);
            map2.put("percentage",getStatus(status));
            list2.add(map2);
        }//[{"createTime","2020-12-20",},{}]

        baseResp.setCode(200L);
        baseResp.setMessage(list2);
        return baseResp;
//        return null;
    }

    public String getStatus(String status){
        Double percent = 0.0;
        if("审核中".equals(status)){
            percent=14.3;
        }else if("审核未通过".equals(status)){
            percent=14.3*2;
        }else if("打款中".equals(status)) {
            percent = 14.3 * 3;
        }else if("打款成功".equals(status)) {
            percent = 14.3 * 4;
        }
        else if("待还款".equals(status)){
            percent=14.3 * 5;
        }else if("已逾期".equals(status)){
            percent=14.3*6;
        }else if("已结清".equals(status)) {
            percent = 100.0;
        }
        return percent+"";
    }

    public String getToken(Cookie[] cookies) {
        String token = null;
        for (Cookie cook : cookies) {
            if (cook.getName().equals("token")) {
                token = cook.getValue();
            }
        }
        return token;
    }

}
